package plus.lgx.proj1

import java.util
import java.util.function.{Consumer, Predicate}
import scala.util.control.Breaks.{break, breakable}

object Book {
  def apply(bookName: String, bookAuthor: String, ISBN: String): Book = new Book(bookName, bookAuthor, ISBN)

  def unapply(arg: Book): Option[(String, String, String)] = Some((arg.bookName, arg.bookAuthor, arg.ISBN))
}

class Book(val bookName: String, val bookAuthor: String, val ISBN: String) {
  override def toString: String = "《" + bookName + "》" + bookAuthor + "，" + ISBN
}

class Library {

  private val availableBooks: util.LinkedList[Book] = new util.LinkedList[Book]
  private val lentBooks: util.LinkedList[Book] = new util.LinkedList[Book]

  private def addNewBook(book: Book): Boolean = {
    availableBooks.add(book)
  }

  private def borrowBook(ISBN: String): Book = {
    val iter = availableBooks.iterator
    var ind = 0
    breakable {
      while (iter.hasNext) {
        if (iter.next.ISBN.equals(ISBN)) break
        ind += 1
      }
      return null
    }
    val book = availableBooks.remove(ind)
    lentBooks.add(book)
    book
  }

  private def returnBook(ISBN: String): Book = {
    var book: Book = null
    lentBooks.removeIf(new Predicate[Book] {
      private var flag = true
      override def test(t: Book): Boolean = {
        if (flag && t.ISBN.equals(ISBN)) {
          book = t
          availableBooks.add(t)
          flag = false
          true
        } else false
      }
    })
    book
  }

  def printBooks(p: Int = 3): Unit = {
    val c = new Consumer[Book] {
      override def accept(t: Book): Unit = println(t)
    }
    if ((p & 1) == 1) {
      println("当前可借书籍列表：")
      availableBooks.forEach(c)
    }
    if ((p & 2) == 2) {
      println("当前已借出书籍列表：")
      lentBooks.forEach(c)
    }
  }

  def add(): Unit = {
    println("正在新增书籍。")
    val book = Book(
      Library.input("请输入书籍名称："),
      Library.input("请输入作者："),
      Library.input("请输入 ISBN 号：")
    )
    println("新增的书籍是：" + book)
    println(if (addNewBook(book)) "新增书籍成功。" else "新增书籍失败。")
    printBooks()
  }

  def borrow(): Unit = {
    println("正在借书。")
    printBooks(1)
    val isbn = Library.input("请输入要借的书籍的 ISBN 号：")
    val book = borrowBook(isbn)
    println(if (book != null) "借书成功。借到的书是：" + book else "借书失败。")
    printBooks()
  }

  def `return`(): Unit = {
    println("正在还书。")
    printBooks(2)
    val isbn = Library.input("请输入要还的书籍的 ISBN 号：")
    val book = returnBook(isbn)
    println(if (book != null) "还书成功。归还的书是：" + book else "还书失败。")
    printBooks()
  }

}

object Library {

  private val s = new java.util.Scanner(System.in)
  private def input(prompt: String): String = {
    print(prompt)
    s.nextLine
  }

  def main(args: Array[String]): Unit = {
    val library = new Library
    while (true) {
      input("\n请输入序号选择要执行的操作：\n1. 查看书籍列表\n2. 添加书籍\n3. 借书\n4. 还书\n[1|2|3|4|?]:") match {
        case "1" => library.printBooks()
        case "2" => library.add()
        case "3" => library.borrow()
        case "4" => library.`return`()
        case _ =>
          println("已退出。")
          return
      }

    }
  }
}
